package com.cdp.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cdp.common.core.domain.AjaxResult;
import com.cdp.common.core.domain.entity.SysUserFeedback;
import com.cdp.common.utils.SecurityUtils;
import com.cdp.system.dto.UserFeedbackDTO;
import com.cdp.system.service.ISysUserFeedbackService;
import com.cdp.system.service.impl.SysUserFeedbackServiceImpl;
import com.cdp.system.vo.SysUserFeedbackVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户反馈
 */
@RestController
@RequestMapping
public class SysUserFeedbackController {

    @Autowired
    private ISysUserFeedbackService sysUserFeedbackService;


    /**
     * 用户反馈表
     * @return
     */
    @PostMapping("saveUserFeedback")
    public AjaxResult saveUserFeedback(@Validated @RequestBody UserFeedbackDTO userFeedbackDTO) {
        SysUserFeedback sysUserFeedback = new SysUserFeedback();
        sysUserFeedback.setCreateTime(new Date());
        sysUserFeedback.setUserId(SecurityUtils.getUserId());
        sysUserFeedback.setContent(userFeedbackDTO.getContent());

        sysUserFeedback.setContactName(userFeedbackDTO.getContactName());
        sysUserFeedback.setSex(userFeedbackDTO.getSex());
        sysUserFeedback.setEmail(userFeedbackDTO.getEmail());
        sysUserFeedback.setPhoneNumber(userFeedbackDTO.getPhoneNumber());

        sysUserFeedbackService.save(sysUserFeedback);

        return AjaxResult.success("反馈成功!");
    }





    /**
     *查询 用户反馈表
     * @return
     */



    @GetMapping("queryUserFeedback11")
    public AjaxResult queryUserFeedback11() {
        LambdaQueryWrapper<SysUserFeedback> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(SysUserFeedback::getCreateTime);
        List<SysUserFeedback> sysUserFeedbacks = sysUserFeedbackService.list(queryWrapper);
        ArrayList<SysUserFeedbackVO> arrayList = new ArrayList<>();

        for (SysUserFeedback sysUserFeedback : sysUserFeedbacks) {
            SysUserFeedbackVO sysUserFeedbackVO = new SysUserFeedbackVO();
            BeanUtils.copyProperties(sysUserFeedback, sysUserFeedbackVO);
            sysUserFeedbackVO.setId(sysUserFeedback.getId());
            // 性别处理
            Integer sex = sysUserFeedback.getSex();
            if (sex == null) {
                sysUserFeedbackVO.setSexStr("未知性别");
            } else {
                switch (sex) {
                    case 1:
                        sysUserFeedbackVO.setSexStr("男");
                        break;
                    case 2:
                        sysUserFeedbackVO.setSexStr("女");
                        break;
                    default:
                        sysUserFeedbackVO.setSexStr("未知性别");
                }
            }

            // 状态处理
            Integer status = sysUserFeedback.getStatus();
            if (status == null) {
                sysUserFeedbackVO.setStatusStr("未知状态");
            } else {
                switch (status) {
                    case 0:
                        sysUserFeedbackVO.setStatusStr("未处理");
                        break;
                    case 1:
                        sysUserFeedbackVO.setStatusStr("已处理");
                        break;
                    default:
                        sysUserFeedbackVO.setStatusStr("未知状态");
                }
            }

            arrayList.add(sysUserFeedbackVO);
        }

        return AjaxResult.success(arrayList);
    }

    @PostMapping("queryUserFeedback")
    public AjaxResult queryUserFeedback2(@RequestBody UserFeedbackDTO userFeedbackDTO) {
     List<SysUserFeedback>  sysUserFeedbacks=  sysUserFeedbackService.queryUserFeedback(userFeedbackDTO);
        ArrayList<SysUserFeedbackVO> arrayList = new ArrayList<>();
        for (SysUserFeedback sysUserFeedback : sysUserFeedbacks) {
            SysUserFeedbackVO sysUserFeedbackVO = new SysUserFeedbackVO();
            BeanUtils.copyProperties(sysUserFeedback, sysUserFeedbackVO);
            sysUserFeedbackVO.setId(sysUserFeedback.getId());
            // 性别处理
            Integer sex = sysUserFeedback.getSex();
            if (sex == null) {
                sysUserFeedbackVO.setSexStr("未知性别");
            } else {
                switch (sex) {
                    case 1:
                        sysUserFeedbackVO.setSexStr("男");
                        break;
                    case 2:
                        sysUserFeedbackVO.setSexStr("女");
                        break;
                    default:
                        sysUserFeedbackVO.setSexStr("未知性别");
                }
            }
            // 状态处理
            Integer status = sysUserFeedback.getStatus();
            if (status == null) {
                sysUserFeedbackVO.setStatusStr("未知状态");
            } else {
                switch (status) {
                    case 0:
                        sysUserFeedbackVO.setStatusStr("未处理");
                        break;
                    case 1:
                        sysUserFeedbackVO.setStatusStr("已处理");
                        break;
                    default:
                        sysUserFeedbackVO.setStatusStr("未知状态");
                }
            }

            arrayList.add(sysUserFeedbackVO);
        }
        return AjaxResult.success(arrayList);

    }


    /**
     *查询 用户反馈表根据个人的userId
     * @return
     */

    @GetMapping("queryUserFeedbackByUserId111")
    public AjaxResult queryUserFeedbackByUserId() {
        Long userId = SecurityUtils.getUserId();

        LambdaQueryWrapper<SysUserFeedback> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserFeedback::getUserId, userId);
        queryWrapper.orderByDesc(SysUserFeedback::getCreateTime);
        List<SysUserFeedback> sysUserFeedbacks = sysUserFeedbackService.list(queryWrapper);
        ArrayList<SysUserFeedbackVO> arrayList = new ArrayList<>();

        for (SysUserFeedback sysUserFeedback : sysUserFeedbacks) {
            SysUserFeedbackVO sysUserFeedbackVO = new SysUserFeedbackVO();
            BeanUtils.copyProperties(sysUserFeedback, sysUserFeedbackVO);

            // 状态字符串处理
            Integer status = sysUserFeedback.getStatus();
            if (status == null) {
                sysUserFeedbackVO.setStatusStr("未知状态");
            } else {
                switch (status) {
                    case 0:
                        sysUserFeedbackVO.setStatusStr("未处理");
                        break;
                    case 1:
                        sysUserFeedbackVO.setStatusStr("已处理");
                        break;
                    default:
                        sysUserFeedbackVO.setStatusStr("未知状态");
                }
            }

            // 性别字符串处理
            Integer sex = sysUserFeedback.getSex();
            if (sex == null) {
                sysUserFeedbackVO.setSexStr("未知性别");
            } else {
                switch (sex) {
                    case 1:
                        sysUserFeedbackVO.setSexStr("男");
                        break;
                    case 2:
                        sysUserFeedbackVO.setSexStr("女");
                        break;
                    default:
                        sysUserFeedbackVO.setSexStr("未知性别");
                }
            }

            arrayList.add(sysUserFeedbackVO);
        }

        return AjaxResult.success(arrayList);
    }


    @PostMapping("queryUserFeedbackByUserId")
    public AjaxResult queryUserFeedbackByUserId2(@RequestBody UserFeedbackDTO userFeedbackDTO) {
        Long userId = SecurityUtils.getUserId();
        userFeedbackDTO.setUserId(userId);
        List<SysUserFeedback>  sysUserFeedbacks=  sysUserFeedbackService.queryUserFeedbackByUserId(userFeedbackDTO);
        ArrayList<SysUserFeedbackVO> arrayList = new ArrayList<>();

        for (SysUserFeedback sysUserFeedback : sysUserFeedbacks) {
            SysUserFeedbackVO sysUserFeedbackVO = new SysUserFeedbackVO();
            BeanUtils.copyProperties(sysUserFeedback, sysUserFeedbackVO);

            // 状态字符串处理
            Integer status = sysUserFeedback.getStatus();
            if (status == null) {
                sysUserFeedbackVO.setStatusStr("未知状态");
            } else {
                switch (status) {
                    case 0:
                        sysUserFeedbackVO.setStatusStr("未处理");
                        break;
                    case 1:
                        sysUserFeedbackVO.setStatusStr("已处理");
                        break;
                    default:
                        sysUserFeedbackVO.setStatusStr("未知状态");
                }
            }

            // 性别字符串处理
            Integer sex = sysUserFeedback.getSex();
            if (sex == null) {
                sysUserFeedbackVO.setSexStr("未知性别");
            } else {
                switch (sex) {
                    case 1:
                        sysUserFeedbackVO.setSexStr("男");
                        break;
                    case 2:
                        sysUserFeedbackVO.setSexStr("女");
                        break;
                    default:
                        sysUserFeedbackVO.setSexStr("未知性别");
                }
            }

            arrayList.add(sysUserFeedbackVO);
        }

        return AjaxResult.success(arrayList);
    }




    @PostMapping("updateUserFeedback")
    public AjaxResult updateUserFeedback(@RequestBody UserFeedbackDTO userFeedbackDTO) {
        if (userFeedbackDTO.getId() == null) {
            return AjaxResult.error("反馈ID不能为空");
        }

        SysUserFeedback sysUserFeedback = new SysUserFeedback();
        sysUserFeedback.setId(userFeedbackDTO.getId());
        sysUserFeedback.setReply(userFeedbackDTO.getReply());
        sysUserFeedback.setStatus(1);  // 新增状态更新
        sysUserFeedback.setUpdateTime(new Date());

        boolean updated = sysUserFeedbackService.updateById(sysUserFeedback);
        return updated ? AjaxResult.success("更新成功") : AjaxResult.error("更新失败");
    }






}
